# Get script directory and change to samples directory
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location $scriptDir

# Initialize Emscripten environment
Write-Host "Initializing Emscripten environment..."
& "../../emsdk/emsdk_env.ps1"

# Check and create output directory
if (-not (Test-Path "dist")) {
    Write-Host "Creating dist directory..."
    New-Item -ItemType Directory -Name "dist" -Force
} else {
    Write-Host "dist directory already exists"
}

# Compile C++ code with emscripten
Write-Host "Compiling C++ code with Emscripten..."
em++ --bind -o ./dist/calculator.js ./src/calculator.cpp `
    -s MODULARIZE=1 `
    -s EXPORT_ES6=1 `
    -s "ENVIRONMENT=web,node" `
    -s SINGLE_FILE=1

# Copy all files from dist directory to electron/dist/web directory
Write-Host "Copying all files from dist to ../../electron/dist/web..."
if (-not (Test-Path "../../electron/dist/web")) {
    New-Item -ItemType Directory -Path "../../electron/dist/web" -Force
}
Copy-Item "dist/*" "../../electron/dist/web/" -Recurse -Force
